const { regeneratorRuntime, throttle, dayjs } = global

Page(global.connect(
  state => ({
    ...state.globalReducer,
    ...state.homeReducer,
    ...state.activityReducer
  }),
  dispatch => ({
    handleRoute: global.handleRoute,
    showSidebar(data) {
      dispatch(global.globalReducer.showSidebar(data))
    },
    hideSidebar(data) {
      dispatch(global.globalReducer.hideSidebar(data))
    },
    handleLicon1Tap(e) {
      console.log('handleLicon1Tap', e)
      this.showSidebar()
    },
    // 审核员活动列表
    async handleLoadListData1(refresh) {
      try {
        // console.log('getAuditProjectList')
        if (!this.uiLoading) {
          // step1
          this.setData({ uiLoading: true })

          // step2
          const { uiCurrentPage, uiPageSize, uiKeyword, uiList } = this.data
          const nextPage = uiCurrentPage + 1
          const condition = {
            id: this.data.minaLoginRes.data.user_id,
            page: nextPage,
            size: uiPageSize,
            keyword: uiKeyword
          }
          const actionRes = await dispatch(global.activityReducer.getAuditActivityList(condition))
          console.log(actionRes)
          if (actionRes.actionStatus === 'SUCCESS') {
            this.setData({
              uiCurrentPage: nextPage,
              uiList: refresh ? actionRes.data : [...uiList, ...actionRes.data]
            })
          } else {
            this.selectComponent('#toast').handleShow({
              message: actionRes.apiMes
            })
            console.error(actionRes)
          }
          // step3
        }
      } catch (error) {
        console.error(error)
      }
      this.setData({ uiLoading: false })
    },
    // 店长活动列表
    async handleLoadListData2(refresh) {
      try {
        console.log('getAuditProjectList', refresh)
        if (!this.uiLoading) {
          // step1
          this.setData({ uiLoading: true })

          // step2
          const { uiCurrentPage, uiPageSize, uiKeyword, uiList } = this.data
          const nextPage = uiCurrentPage + 1
          const condition = {
            // id: 10, // todo this.data.minaLoginRes.data.user_id,
            id: this.data.minaLoginRes.data.user_id,
            page: nextPage,
            size: uiPageSize,
            keyword: uiKeyword
          }
          const actionRes = await dispatch(global.activityReducer.getStoremanagerActivityList(condition))
          console.log(actionRes)
          if (actionRes.actionStatus === 'SUCCESS') {
            this.setData({
              uiCurrentPage: nextPage,
              uiList: refresh ? actionRes.data : [...uiList, ...actionRes.data]
            })
          } else {
            this.selectComponent('#toast').handleShow({
              message: actionRes.apiMes
            })
            console.error(actionRes)
          }
          // step3
        }
      } catch (error) {
        console.error(error)
      }
      this.setData({ uiLoading: false })
    },
  })
)({
  data: {
    uiFnLock: false,
    uiLoading: false,
    uiList: [],
    uiCurrentPage: 0,
    uiPageSize: 2,
    uiKeyword: '',
    uiShowSearch: false
  },
  handleRicon1Tap(e) {
    const uiShowSearch = !this.data.uiShowSearch
    this.setData({
      uiShowSearch
    })
  },
  handleSearchConfirm(e) {
    console.log('handleSearchConfirm', e)
    const uiKeyword = e.detail.detail.value
    this.setData({ uiKeyword })
    this.handleReloadListData(true)
  },
  onLoad() {
    console.log('index onload', this)
  },
  onShow() {
    // console.log('index onshow')
    global.waitUntilLogin(this.handleReloadListData)
  },
  handleReloadListData(refresh) {
    this.setData({
      uiList: [],
      uiCurrentPage: 0,
      uiPageSize: 5,
    })
    if (this.data.minaLoginRes.data.uiUserType === '审核员') {
      this.handleLoadListData1(refresh)
    }
    if (this.data.minaLoginRes.data.uiUserType === '店长') {
      this.handleLoadListData2(refresh)
    }
  },
  handleActivityTap(e) {
    console.log('handleActivityTap', e.currentTarget.dataset.item)
    this.handleRoute({
      route: 'activity-detail', query: {
        activity_id: e.currentTarget.dataset.item.id
      }
    })
  },
  handleScrollLoad(e) {
    if (this.data.minaLoginRes.data.uiUserType === '审核员') {
      this.handleLoadListData1(false)
    }
    if (this.data.minaLoginRes.data.uiUserType === '店长') {
      this.handleLoadListData2(false)
    }
  },
  handleCatchTest(e) {
    console.log('handleCatchTest', e.currentTarget.dataset)
    const { status, order_no, store_id, id } = e.currentTarget.dataset.detail

    console.log(status)
    if (Number(status) === 0) {
      global.handleRoute({
        route: 'make-order-step1',
        query: { status, order_no, store_id }
      })
    } else {
      global.handleRoute({
        route: 'order-detail',
        query: { activity_id: id, status, order_no, store_id }
      })
    }
    // todo
    // if (Number(status) === 1) {
    //   global.handleRoute({
    //     route: 'order-detail',
    //     query: { status, order_no, store_id }
    //   })
    // }
    // if (Number(status) === 2) {
    //   global.handleRoute({
    //     route: 'order-detail',
    //     query: { status, order_no, store_id }
    //   })
    // }
  }
  // handleScrollLoad: throttle(function (e) {
  //   this.handleLoadListData()
  // }, 500)
}))